package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.manger.BeautyEscortConfigMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_ACCEPT_DISPATCH_BEAUTY, desc = "接镖+派美女")
public class BeautyEscortAcceptDispatchCmd extends AbstractCommand {
    private int client;
    private int type;
    private List<Integer> beautyList;
    private boolean autoAccelerate;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortAcceptDispatchReqMsg reqMsg = BeautyEscortProto.BeautyEscortAcceptDispatchReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getTypeId();
        client = reqMsg.getClient();
        beautyList = reqMsg.getBeautyList();
        autoAccelerate = reqMsg.getAutoAccelerate();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        //商会等级限制
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
        if(UnionConfigMgr.getUnionLv(unionInfo.getExperience()) < GameConfig.ESCORT_OPEN_UNION_LEVEL) {
            ret = GameErrorCode.E_BEAUTY_ESCORT_UNION_LEVEL_NO_ENOUGH;
            return;
        }
        //时间限制
        if(!ConfigMgr.isEscortTimeInDay()){
            ret = GameErrorCode.E_BEAUTY_ESCORT_NO_IN_ESCORT_TIME;
            return;
        }

       if(!ConfigMgr.getDartTypeInfoMap().containsKey(type) || !BeautyEscortConfigMgr.getClientInfoMap().containsKey(client)){
           ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
           return;
       }
       if(autoAccelerate) {
           if (player.getUserInfo().getTitleId() < GameConfig.ESCORT_AUTOMATIC_OPEN_LEVEL) {
               ret = GameErrorCode.E_TITLE_LEVEL_NO_ENOUGH;
               return;
           }
       }
       ret = player.getModule(BeautyEscortModule.class).acceptAndDispatchBeauty(type, client, beautyList, autoAccelerate);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortAcceptDispatchRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortAcceptDispatchRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_ACCEPT_DISPATCH_BEAUTY, respMsg);
        }
    }
}
